<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <!--<div class="layui-col-sm12 layui-col-md3 layui-col-lg2">-->
        <!--  <div class="layui-card">-->
        <!--    <div class="layui-card-body mini-bar">-->
        <!--      <div class="ztree" id="deptTree"></div>-->
        <!--    </div>-->
        <!--  </div>-->
        <!--</div>-->
        <div class="layui-col-sm12 layui-col-md9 layui-col-lg12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="layui-form toolbar">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <input id="name" class="layui-input layui-input-inline" type="text" placeholder="账号/姓名/手机号"/>
                            </div>
                            <div class="layui-inline">
                                <input id="timeLimit" class="layui-input layui-input-inline" type="text" placeholder="注册时间"/>
                            </div>
                            <div class="layui-inline">
                                <button id="btnSearch" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
                                <button id="btnExp" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>
                            </div>
                        </div>
                    </div>
                    <table class="layui-table" id="SysUserTable" lay-filter="SysUserTable"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="SysUserTableToolBar">
    {php} if(in_array("admin/system.sysuser/add",session('menunames'))){ {/php}
    <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add"><i class="layui-icon">&#xe654;</i>添加</a>
    {php} } {/php}
    {php} if(in_array("admin/system.sysuser/edit",session('menunames'))){ {/php}
    <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="edit"><i class="layui-icon">&#xe642;</i>修改</a>
    {php} } {/php}
    {php} if(in_array("admin/system.sysuser/delete",session('menunames'))){ {/php}
    <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del"><i class="layui-icon">&#xe640;</i>删除</a>
    {php} } {/php}
    {php} if(in_array("admin/system.sysuser/reset",session('menunames'))){ {/php}
    <a class="layui-btn layui-btn-green layui-btn-sm" lay-event="reset"><i class="layui-icon">&#xe673;</i>重置密码</a>
    {php} } {/php}
    <a class="layui-btn layui-btn-sm" lay-event="reload"><i class="layui-icon layui-icon-refresh"></i>重载</a>
    <a class="layui-btn layui-btn-sm layui-btn-normal" lay-event="clear"><i class="layui-icon layui-icon-delete"></i>清除缓存</a>
</script>
<script type="text/html" id="SysUserTableOptions">
    <input type="checkbox" lay-filter="status" value="{{d.userId}}" lay-skin="switch" lay-text="正常|冻结" {{d.status=='ENABLE'?'checked':''}}/>
</script>
<script>
    var SysUserTable = layui.table.render({
        elem: "#SysUserTable",
        url: '{:url("/admin/system.sysuser/index")}',
        method: "post",
        where: {sort: 'id', order: 'desc'},
        request: {
            limitName: 'pageSize'
        },
        page: true,
        //skin: 'line',
        even: true, //开启隔行背景
        size: 'sm', //小尺寸的表格
        limit: '10',
        toolbar: '#SysUserTableToolBar',
        loading: true,
        height: 'full-160',
        cellMinWidth: 100,
        response: {
            statusCode: 1 //重新规定成功的状态码为 200，table 组件默认为 0
        },
        parseData: function (res) {
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.data //解析数据列表
            }
        },
        cols: [[
            {type: 'radio'},
            {
                field: "account",
                title: '账号',
                width: 100, sort: true, filter: true
            },
            {
                field: "nickname",
                title: '昵称',
                width: 100, sort: true, filter: true
            },
            {
                field: "sex",
                title: '性别',
                width: 70,
                align: 'center', sort: true, filter: true,
                templet: function (val) {
                    if (val.sex == 1) {
                        return "男";
                    } else {
                        return "女";
                    }
                }
            },
            {
                field: "email",
                title: '邮件',
                width: 150, sort: true, filter: true
            },
            {
                field: "mobile",
                title: '电话',
                width: 120, sort: true, filter: true
            },
            {
                field: "create_time",
                title: '创建时间',
                width: 160, sort: true, filter: true,
                templet: function (val) {
                    if (val.create_time != null && val.create_time != "") {
                        return top.formatDateTime(val.create_time);
                    } else {
                        return "空";
                    }
                }
            },
            {
                field: "update_time",
                title: '修改时间',
                width: 160, sort: true, filter: true,
                templet: function (val) {
                    if (val.update_time != null && val.update_time != "") {
                        return top.formatDateTime(val.update_time);
                    } else {
                        return "空";
                    }
                }
            },
            {
                field: "roles",
                title: '所属角色',
                width: 200, sort: true, filter: true,
                templet: function (d) {
                    var tempstr = [];
                    $.each(d.roles, function (index, item) {
                        tempstr.push(item.title);
                    });
                    return tempstr.join(',');
                }
            },
            {
                field: "status",
                title: '状态',
                width: 70, sort: true, filter: true,
                templet: function (val) {
                    if (val.status == "1") {
                        return '<span class="layui-badge layui-bg-green">启用</span>';
                    } else {
                        return '<span class="layui-badge layui-bg-red">禁用</span>';
                    }
                }
            },
            {
                field: "remark",
                title: '备注',
                align: 'left', sort: true, filter: true
            },
            {field: "", title: '操作', toolbar: "#tableToolBar", width: 220, fixed: 'right', align: 'center', hide: true},
        ]],
        filter: {
            // items: ['column', 'data', 'condition', 'editCondition', 'excel', 'clearCache'],
            cache: true
        },
        done: function (res, curr, count) {
            layui.soulTable.render(this);
        }
    })
    layui.table.on('toolbar(SysUserTable)', function (obj) {
        let data = layui.table.checkStatus(obj.config.id).data[0];
        if (obj.event == 'add') {
            $.get('{:url("/admin/system.sysuser/edit")}', {}, function (str) {
                var indexlayer = layui.layer.open({
                    title: "添加",
                    offset: 't',
                    area: ['530px', '500px'],
                    shadeClose: true,
                    type: 1,
                    anim: parseInt(Math.random() * 6),
                    resize: false,
                    btn: ['保存', '取消'],
                    content: str,
                    yes: function (index, layero) {
                        //return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                        var data = $("#SysUserForm").serializeObject();
                        var dTree_roleids_params = layui.dtree.getCheckbarNodesParam("dTree_roleids");
                        // console.log(params);
                        var roleIds = [];
                        $.each(dTree_roleids_params, function (index, item) {
                            roleIds.push(item.nodeId);
                        });
                        data.roleIds = roleIds;
                        $.ajax({
                            async: true,//默认true为异步
                            url: '{:url("/admin/system.sysuser/add")}',
                            type: "post",
                            data: data,
                            dataType: "json",
                            beforeSend: function (xhr) {
                            },
                            success: function (res, status, xhr) {
                                layer.msg(res.msg);
                                if (res.code == 1) {
                                    layui.table.reload('SysUserTable', {});
                                    //layui.layer.close(indexlayer);
                                }
                            },
                            error: function (xhr, status, error) {
                            }
                        });
                    },
                    success: function (layero, index) {
                        yq_dtree('dTree_roleids', {
                            url: '{:url("/admin/widget.sysrole/getAllList")}',
                            checkbar: true
                        }, function () {
                        });
                    },
                    btn2: function (index, layero) {
                        //按钮【按钮二】的回调
                        //return false 开启该代码可禁止点击该按钮关闭
                    },
                    btn3: function (index, layero) {
                        //按钮【按钮三】的回调
                        //return false 开启该代码可禁止点击该按钮关闭
                    },
                    cancel: function () {
                        //右上角关闭回调
                        //return false 开启该代码可禁止点击该按钮关闭
                    }
                })
            });
        }
        if (obj.event == 'edit') {
            if (data != null) {
                $.get('{:url("/admin/system.sysuser/edit")}', {"id": data.id}, function (str) {
                    var indexlayer = layui.layer.open({
                        title: "修改",
                        offset: 't',
                        area: ['530px', '500px'],
                        shadeClose: true,
                        type: 1,
                        anim: parseInt(Math.random() * 6),
                        resize: false,
                        btn: ['保存', '取消'],
                        content: str,
                        yes: function (index, layero) {
                            //return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                            var data = $("#SysUserForm").serializeObject();
                            var dTree_roleids_params = layui.dtree.getCheckbarNodesParam("dTree_roleids");
                            // console.log(params);
                            var roleIds = [];
                            $.each(dTree_roleids_params, function (index, item) {
                                roleIds.push(item.nodeId);
                            });
                            data.roleIds = roleIds;
                            $.ajax({
                                async: true,//默认true为异步
                                url: '{:url("/admin/system.sysuser/edit")}',
                                type: "post",
                                data: data,
                                dataType: "json",
                                beforeSend: function (xhr) {
                                },
                                success: function (res, status, xhr) {
                                    layer.msg(res.msg);
                                    if (res.code == 1) {
                                        layui.table.reload('SysUserTable', {});
                                        layer.close(indexlayer);
                                    }
                                },
                                error: function (xhr, status, error) {
                                }
                            });
                        },
                        success: function (layero, index) {
                            var enhance = new layui.enhanceform({
                                elem: '#SysUserForm' //表单选择器
                            });
                            enhance.filling(data);//其中jsonData为表单数据的json对象
                            yq_dtree('dTree_roleids', {
                                url: '{:url("/admin/widget.sysrole/getAllList")}',
                                checkbar: true
                            }, function () {
                                let ids = [];
                                $.each(data.menus, function (index, item) {
                                    ids.push(item.id);
                                });
                                // layui.dtree.dataInit("dTree_roleids", ids.join(','));
                                var params = layui.dtree.chooseDataInit("dTree_roleids", ids.join(','));
                                layui.dtree.selectVal("dTree_roleids"); // 也可以在这里指定，第二个参数如果不填，则会自动读取
                            });
                            layui.form.render();
                        },
                        btn2: function (index, layero) {
                            //按钮【按钮二】的回调
                            //return false 开启该代码可禁止点击该按钮关闭
                        },
                        btn3: function (index, layero) {
                            //按钮【按钮三】的回调
                            //return false 开启该代码可禁止点击该按钮关闭
                        },
                        cancel: function () {
                            //右上角关闭回调
                            //return false 开启该代码可禁止点击该按钮关闭
                        }
                    })
                });
            } else {
                layer.msg('请勾选记录！');
            }
        }
        if (obj.event == 'del') {
            if (data != null) {
                layui.layer.confirm('确定移除此信息？', {icon: 2, title: '提示信息'}, function (index) {
                    $.ajax({
                        type: "post",
                        url: '{:url("/admin/system.sysuser/delete")}',
                        data: {'id': data.id},
                        dataType: 'json',
                        async: false,
                        success: function (d) {
                            if (d.code == 200) {
                                layui.layer.msg("删除成功");
                                layui.table.reload('SysUserTable', {});
                            } else {
                                layui.layer.msg(d.msg);
                            }
                        }
                    })
                    layer.close(index);
                });
            } else {
                layer.msg('请勾选记录！');
            }
        }
        if (obj.event == 'reset') {
            console.log(loginuser);
            if (data != null) {
                if (data.username == "admin" && loginuser.username != "admin") {
                    layer.msg('只有超级管理员用户可以修改admin密码！');
                } else {
                    layer.prompt({
                        formType: 2,
                        value: '',
                        title: '请输入',
                        area: ['400px', '30px'] //自定义文本域宽高
                    }, function (value, index, elem) {
                        $.ajax({
                            type: "post",
                            url: '{:url("/admin/system.sysuser/reset")}',
                            data: {'id': data.id, 'password': value},
                            dataType: 'json',
                            async: false,
                            success: function (d) {
                                if (d.code == 200) {
                                    layui.layer.msg("成功");
                                    layui.table.reload('SysUserTable', {});
                                } else {
                                    layui.layer.msg(d.msg);
                                }
                            }
                        })
                        layer.close(index);
                    });
                }
            } else {
                layer.msg('请勾选记录！');
            }
        }
        if (obj.event == 'reload') {
            layui.table.reload('SysUserTable');
        }
        if (obj.event == 'clear') {
            layui.soulTable.clearCache(SysUserTable.config.id)
            layer.msg('已还原！', {icon: 1, time: 1000})
        }
    });
</script>
</html>